// pages/login/login.js
const app = getApp();
const db = wx.cloud.database().collection("administer")
Page({
  data: {
    Password: '',
    UserName: '',
  },
  login() {
    const { UserName, Password } = this.data;
    console.log(UserName, Password)
    if (UserName.trim() === '' || Password.trim() === '') {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'none'
      });
      return;
    }

    // 查询数据库
    db.where({
      _id: UserName,
      password: Password
    }).get({
      success: res => {
        if (res.data.length > 0) {
          wx.showToast({
            title: '登录成功',
            icon: 'success'
          });
          wx.navigateTo({
            url: '/pages/admini/admini'
          });
        } else {
          wx.showToast({
            title: '登录失败，请检查用户名和密码',
            icon: 'none'
          });
        }
      },
      fail: err => {
        console.error('查询失败', err);
        wx.showToast({
          title: '登录失败，请稍后重试',
          icon: 'none'
        });
      }
    });
  }
})